package org.jeecg.desen.cretab.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.jeecg.modules.meta.entity.SysTableinfo;
import org.jeecg.modules.meta.mapper.SysTableinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;


/**
 * 根据状态和日期跟新脱敏库
 */
@Service("desensHiveCreateTableByStatusServiceImpl")
public class DesensHiveCreateTableByStatusServiceImpl extends AbstractHiveCreateService {

    @Autowired
    SysTableinfoMapper sysTableinfoMapper;

    @Override
    IPage<SysTableinfo> getTableList(IPage page, Date date) {
        return getDelListByTime(page, date);
    }

    private IPage<SysTableinfo> getDelListByTime(IPage page, Date date) {
        QueryWrapper<SysTableinfo> wrapper = new QueryWrapper<>();
        wrapper.between("fd_updatetime", DateUtil.formatDate(date) + " 00:00:00", DateUtil.formatDate(date) + " 23:59:59");
        return sysTableinfoMapper.selectPage(page, wrapper);
    }

    /**
     * 根据状态更新
     *
     * @param sysTableinfos
     */
    @Override
    void analysisTable(List<SysTableinfo> sysTableinfos) {
        for (SysTableinfo sysTableinfo : sysTableinfos) {
            if (sysTableinfo.getFdStatus().equals("0")) {//正常的 先删后建
                //删除表
                dropTable(sysTableinfo);
                //新建表
                createTableByLike(sysTableinfo);
            }
            //删除的不做处理
        }
    }

}
